<?php

namespace common\services;

use common\models\Material;
use Yii;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

class TallyMaterialServices extends BaseService
{
    public static function add($formData = []){
        $model = new Material();
        $model->load($formData,'');
        if ($model->validate() && $model->save()) {
            return $model->id ;
        }else{
            Yii::error('TallyStore insert error:'.current($model->getFirstErrors()).';post:'.Json::encode($formData,JSON_UNESCAPED_UNICODE));
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }
    private static function getQuery($condition){
        $query = Material::find()
            ->select('*');

        $tallyId = ArrayHelper::getValue($condition,'tally_id','');
        if($tallyId){
            $query->andFilterWhere(['=','tally_id',$tallyId]);
        }

        $memo = ArrayHelper::getValue($condition,'memo','');
        if($memo){
            $query->andFilterWhere(['like','memo',$memo]);
        }

        $titleLike = ArrayHelper::getValue($condition,'titleLike');
        if($titleLike){
            $query->andFilterWhere([
                'like','title',$titleLike
            ]);
        }

        return $query;
    }

    public static function getList($condition): array
    {
        $query = self::getQuery($condition);
        $count = $query->count();
        $pageSize = $_SERVER['PAGE_SIZE'] ?: 10 ;
        if(isset($condition['page_limie'])){
            $pageSize = $condition['page_limie'];
        }
        $pagination = new Pagination([
            'defaultPageSize' => $pageSize,
            'totalCount' => $count
        ]);
        $pagination->validatePage=false;
        $orderByStr = 'id desc';
        $list = $query
            ->orderBy($orderByStr)
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

        $list = $list ?: [];
        return [
            'data'       => $list,
            'pageInfo'   => [
                'totalCount' => intval($count) ,
                'pageSize'   => intval($pageSize)
            ]
        ];

    }

    public static function update($condition = [], $formData = [])
    {
        $model = Material::find()->where($condition)->one();
        if(!$model){
            self::setError('材料不存在');
            return false ;
        }
        $model->load($formData,'');
        if ($model->validate() && $model->save()) {
            return true ;
        }else{
            Yii::error('TallyStore insert error:'.current($model->getFirstErrors()).';post:'.Json::encode($formData,JSON_UNESCAPED_UNICODE));
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }

    public static function delete(array $condition = []): bool
    {
        $model = Material::find()->where($condition)->one();
        if(!$model){
            self::setError('成员不存在');
            return false ;
        }
        if ($model->delete()) {
            return true ;
        }else{
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }

    public static function getListAll(array $array)
    {
        return self::getQuery($array)->all();
    }
}